package com.hk.dao;

import com.hk.entity.Course;
import com.hk.entity.Sc;
import com.hk.utils.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class ScDao {
    public void add(Sc sc) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="insert into student_course(stuId,cId,score) values (?,?,?)";
        queryRunner.update(sql,sc.getStuId(),sc.getcId(),sc.getScore());
    }
    public void delete(Integer scId) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="delete from student_course where scId=?";
        queryRunner.update(sql,scId);
    }
    public void update(Sc sc) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="update student_course set stuId=?,cId=?,score=?where scId=?";
        queryRunner.update(sql,sc.getStuId(),sc.getcId(),sc.getScore(),sc.getScId());

    }
    public List<Sc> list(Sc sc) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="select * from student_course";
        List<Sc> list=queryRunner.query(sql,new BeanListHandler<>(Sc.class));
        return list;
    }
    public Sc findById(Integer scId) throws Exception {
        QueryRunner queryRunner=new QueryRunner(PropertiesUtils.getDataSource());
        String sql="select * from student_course where scId=?";
        Sc sc=queryRunner.query(sql,new BeanHandler<>(Sc.class),scId);
        return sc;
    }
}
